package com.share.device.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.share.common.core.utils.DateUtils;
import com.share.device.domain.Cabinet;
import com.share.device.domain.CabinetSlot;
import com.share.device.domain.PowerBank;
import com.share.device.mapper.CabinetMapper;
import com.share.device.mapper.CabinetSlotMapper;
import com.share.device.service.ICabinetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 充电宝柜机业务层
 *
 * @author         
 * @date 2025-06-24
 */
@Service
public class CabinetServiceImpl extends ServiceImpl<CabinetMapper, Cabinet> implements ICabinetService
{
    @Autowired
    private CabinetMapper cabinetMapper;
    @Autowired
    private CabinetSlotMapper cabinetSlotMapper;

    @Autowired
    private PowerBankServiceImpl powerBankService;

    /**
     * 查询充电宝柜机
     *
     * @param id 充电宝柜机主键
     * @return 充电宝柜机
     */
    @Override
    public Cabinet selectCabinetById(Long id)
    {
        return cabinetMapper.selectCabinetById(id);
    }

    /**
     * 查询充电宝柜机列表
     *
     * @param cabinet 充电宝柜机
     * @return 充电宝柜机
     */
    @Override
    public List<Cabinet> selectCabinetList(Cabinet cabinet)
    {
        return cabinetMapper.selectCabinetList(cabinet);
    }

    /**
     * 新增充电宝柜机
     *
     * @param cabinet 充电宝柜机
     * @return 结果
     */
    @Override
    public int insertCabinet(Cabinet cabinet)
    {
        cabinet.setCreateTime(DateUtils.getNowDate());
        return cabinetMapper.insertCabinet(cabinet);
    }

    /**
     * 修改充电宝柜机
     *
     * @param cabinet 充电宝柜机
     * @return 结果
     */
    @Override
    public int updateCabinet(Cabinet cabinet)
    {
        cabinet.setUpdateTime(DateUtils.getNowDate());
        return cabinetMapper.updateCabinet(cabinet);
    }

    /**
     * 批量删除充电宝柜机
     *
     * @param ids 需要删除的充电宝柜机主键
     * @return 结果
     */
    @Override
    public int deleteCabinetByIds(Long[] ids)
    {
        return cabinetMapper.deleteCabinetByIds(ids);
    }

    /**
     * 删除充电宝柜机信息
     *
     * @param id 充电宝柜机主键
     * @return 结果
     */
    @Override
    public int deleteCabinetById(Long id)
    {
        return cabinetMapper.deleteCabinetById(id);
    }
    @Override
    public Map<String, Object> getAllInfo(Long id) {
        // 查询柜机信息
        Cabinet cabinet = this.getById(id);

        // 查询插槽信息
        List<CabinetSlot> cabinetSlotList = cabinetSlotMapper.selectList(new LambdaQueryWrapper<CabinetSlot>().eq(CabinetSlot::getCabinetId, cabinet.getId()));
        // 获取可用充电宝id列表
        List<Long> powerBankIdList = cabinetSlotList.stream().filter(item -> null != item.getPowerBankId()).map(CabinetSlot::getPowerBankId).collect(Collectors.toList());
        if(!CollectionUtils.isEmpty(powerBankIdList)) {
            List<PowerBank> powerBankList = powerBankService.listByIds(powerBankIdList);
            Map<Long,PowerBank> powerBankIdToPowerBankMap = powerBankList.stream().collect(Collectors.toMap(PowerBank::getId, PowerBank -> PowerBank));
            cabinetSlotList.forEach(item -> item.setPowerBank(powerBankIdToPowerBankMap.get(item.getPowerBankId())));
        }

        // 创建结果map
        Map<String, Object> result = new HashMap<>();
        result.put("cabinet", cabinet);
        result.put("cabinetSlotList", cabinetSlotList);
        return result;
    }
}
